package com.own.business.system.menu.entity.dto;

import com.own.business.system.menu.entity.bo.SystemRoleBo;
import com.own.component.common.base.entity.BaseDto;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * SystemRoleDto
 *
 * @author chenxueli
 * @date 2024-03-31 09:22:58
 */
@Schema(title = "系统角色" + ConstantBeanSuffix.DTO)
@Getter
@Setter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class SystemRoleDto extends BaseDto<SystemRoleBo> implements Serializable {

    @Schema(title = "角色名称")
    private String name;

    @Schema(title = "客户端id")
    private String clientId;

    @Schema(title = "是否禁用 1=禁用 0=可用")
    private Integer isDisable;

    @Schema(title = "权限列表")
    private List<SystemRolePermissionDto> permissions;

    /**
     * 初始化
     */
    @Override
    protected void init() {
        super.init();
        if (isDisable == null) {
            isDisable = 0;
        }
        isDisable = isDisable % 2;
    }

    /**
     * 验证数据
     */
    @Override
    public void check() {
        super.check();
        if (StringUtils.isBlank(this.name)) {
            throw new BusinessSimpleException("角色名称不能为空");
        }
        if (this.name.length() > 16) {
            throw new BusinessSimpleException("角色名称不能超过16个字符");
        }
        if (StringUtils.isBlank(clientId)) {
            throw new BusinessSimpleException("客户端不能为空");
        }
        // 客户端名称不能超过32位
        if (clientId.length() > 32) {
            throw new BusinessSimpleException("客户端名称长度不能超过32位");
        }
    }

    /**
     * 转业务层对象
     *
     * @param entity 原业务层对象
     * @return 业务层对象
     */
    @Override
    public SystemRoleBo toBo(SystemRoleBo entity) {
        if (entity == null) {
            entity = new SystemRoleBo();
        }
        entity.setName(this.name);
        entity.setClientId(this.clientId);
        entity.setIsDisable(this.isDisable);
        entity.setPermissions(this.permissions.stream().map(SystemRolePermissionDto::toBo).toList());
        return super.toBo(entity);
    }

}

